package com.han.controllers.export;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jmesa.core.filter.MatcherKey;
import org.jmesa.core.filter.NumberFilterMatcher;
import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.editor.CellEditor;
import org.jmesa.view.html.HtmlBuilder;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;

import com.han.bom.ExamResponse;
import com.han.bom.Professor;
import com.han.dao.concrete.product.ExamResponseDAO;

/**
 * Servlet implementation class JMesaExportServlet
 */
@WebServlet("/Professor/JMesaExportAllServlet")
public class JMesaExportAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JMesaExportAllServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		
		Professor professor = (Professor) session.getAttribute("professor");
		
		ExamResponseDAO examresponsedao = new ExamResponseDAO();

		ArrayList<ExamResponse> allResponses = new ArrayList<ExamResponse>();
		
		try {
			ArrayList<String> examResponseIDs = examresponsedao.retrieveAllResponses(professor.getUserID());
			
			for(String responseID:examResponseIDs){
				
				ExamResponse tempER = examresponsedao.retrieveResponseById(responseID);				

				allResponses.add(tempER);
				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		TableModel tableModel = new TableModel("table", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(allResponses);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("Examinee-Exam Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column id = new Column("studentID").title("Examinee ID");
			Row.addColumn(id);
	
			Column lastName = new Column("examID").title("Exam ID");
			Row.addColumn(lastName);
	
			Column term = new Column("dateTaken").title("Date Taken");
			Row.addColumn(term);
	
			Column career = new Column("grade").title("Grade");
			tableModel.addFilterMatcher(new MatcherKey(Double.class, "grade"), new NumberFilterMatcher("###.##"));
			Row.addColumn(career); 
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			
			request.setAttribute("table",export);
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow htmlRow = new HtmlRow();
			htmlTable.setRow(htmlRow);
						
			HtmlColumn studentIDCol = new HtmlColumn("studentID").title("Examinee ID");
	    	htmlRow.addColumn(studentIDCol); 
	
			HtmlColumn examIDCol = new HtmlColumn("examID").title("Exam ID");
	    	htmlRow.addColumn(examIDCol);
	    	
	    	HtmlColumn dateCol = new HtmlColumn("dateTaken").title("Date Taken");
	    	htmlRow.addColumn(dateCol);
	
	    	HtmlColumn gradeCol = new HtmlColumn("grade").title("Grade");
	    	tableModel.addFilterMatcher(new MatcherKey(Double.class, "grade"), new NumberFilterMatcher("###.##"));
	    	htmlRow.addColumn(gradeCol);
	    	
	    	HtmlColumn viewLink = new HtmlColumn("name").title("View");
	    	viewLink.setCellEditor(new CellEditor() {             
				public Object getValue(Object item, String property, int rowcount) {                 
	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
	    			HtmlBuilder html = new HtmlBuilder();
	    			ExamResponse er = (ExamResponse)item;
	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
	    			html.append("View Response");
	    			html.aEnd();
	    			return html.toString();             
	    			}         
	    		});
	    	viewLink.setSortable(false);
	    	viewLink.setFilterable(false);
	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			
			request.setAttribute("table",html);
			RequestDispatcher rd = request.getRequestDispatcher("Report_StudentsScore.jsp");
			rd.forward(request, response);	
		}
	}

}
